package com.framework.bdf4j.sysadm.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.bdf4j.comm.persist.PageDomain;
import com.framework.bdf4j.comm.util.R;
import com.framework.bdf4j.comm.util.StringUtils;
import com.framework.bdf4j.sysadm.entity.SysLog;
import com.framework.bdf4j.sysadm.service.SysLogService;
import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.utils.AttachmentExportUtil;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 日志表 前端控制器
 * </p>
 *
 */
@Controller
@AllArgsConstructor
@RequestMapping("/admin/log")
@Api(value = "log", tags = "日志管理模块")
public class SysLogController {
    private final SysLogService sysLogService;

    // 跳转到日志列表页
    @GetMapping("/index")
    public String goLog() {
        return "sysadm/syslog/syslogList";
    }

    /**
     * 简单分页查询
     *
     * @param page
     *            分页对象
     * @param sysLog
     *            系统日志
     * @return
     */
    @ResponseBody
    @GetMapping("/page")
    public R getLogPage(Page page, PageDomain domain, SysLog syslog) {
        OrderItem orderitem = StringUtils.getOrderItem(domain);
        if (orderitem != null) {
            page.addOrder(orderitem);
        }
        return R.ok(sysLogService.getSysLogPage(page, syslog));
    }

    /**
     * 删除日志
     *
     * @param id
     *            ID
     * @return success/false
     */
    @ResponseBody
    @DeleteMapping("/{id}")
    @com.framework.bdf4j.annotations.SysLog("删除日志")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(sysLogService.removeById(id));
    }

    @ResponseBody
    @PostMapping("/clean")
    @com.framework.bdf4j.annotations.SysLog("清空日志")
    @PreAuthorize("@pms.hasPermission('sys_log_del')")
    public R clean() {
        return R.ok(sysLogService.remove(Wrappers.emptyWrapper()));
    }

    /**
     * 导出Excel文件
     *
     */
    @GetMapping("/export")
    public void doExcel(HttpServletResponse response, SysLog syslog) {
        Workbook workbook = DefaultExcelBuilder.of(SysLog.class).build(sysLogService.getSysLogList(syslog));
        AttachmentExportUtil.export(workbook, "日志数据", response);
    }

    @GetMapping("/detail/{operId}")
    public String detail(@PathVariable("operId") Long operId, ModelMap mmap) {
        mmap.put("operLog", sysLogService.getById(operId));
        return "sysadm/syslog/detail";
    }

}
